import unittest
import requests

# 新建测试类，继承unittest.TestCase
class TestLogin(unittest.TestCase):

    # setUp：以test开头的方法执行之前，首先会被执行
    def setUp(self):
        # 获取session对象
        self.session = requests.session()
        # 登录url
        self.url_addadmin = "http://localhost:5001/addadmin"
        self.url_deleteadmin = "http://localhost:5001/deleteadmin"
        self.url_updateadmin = "http://localhost:5001/updateadmin"
        self.url_findadmin = "http://localhost:5001/query"

    # tearDown：以test开头的方法执行之后，会被执行
    def tearDown(self):
        # 关闭session
        self.session.close()

    # 测试用例方法1：添加成功
    def test_addadmin_success(self):
        # 准备测试数据 - 使用一个新的未存在的账号
        data = {
            "account": "newadmin8",  # 修改为新的账号
            "password": "000000",
            "rid": "other",
            "createTime": "2024-01-01 00:00:00",
            "describe": "普通管理员",
            "status": "1"
        }
        # 发起添加admin的请求
        response = self.session.post(self.url_addadmin, json=data)

        # 检查响应状态码是否为200
        self.assertEqual(response.status_code, 200)

        # 解析响应内容
        response_data = response.json()

        print(response_data)
        # 断言状态为success
        self.assertEqual(response_data['status'], 'success')

    # 测试用例方法2：添加失败
    def test_addadmin_fail(self):
        # 准备测试数据
        data = {
            "account": "admin1200",
            "password": "000000",
            "rid": "other",
            "createTime": "2024-01-01 00:00:00",
            "describe": "普通管理员",
            "status": "1"
        }

        # 发起添加admin的请求
        response = self.session.post(self.url_addadmin, json=data)

        # 检查响应状态码是否为200
        self.assertEqual(response.status_code, 200)

        # 解析响应内容
        response_data = response.json()

        print(response_data)
        # 断言状态为fail
        self.assertEqual(response_data['status'], 'fail')

    # 测试用例方法3：删除成功
    def test_delete_success(self):
        # 构造管理员id数据 - 使用确实存在的ID
        admin_id = 8  # 修改为一个存在的ID

        # 发起删除管理员请求
        delete_data = {'id': admin_id}
        response = self.session.delete(self.url_deleteadmin, json=delete_data)

        # 解析响应内容
        response_data = response.json()
        print(response_data)

        # 断言状态为success
        self.assertEqual(response_data['status'], 'success')

    # 测试用例方法4：删除失败
    def test_delete_fail(self):
        # 构造管理员id数据 - 使用不存在的ID
        admin_id = 999  # 修改为一个不存在的ID

        # 发起删除管理员请求
        delete_data = {'id': admin_id}
        response = self.session.delete(self.url_deleteadmin, json=delete_data)

        # 解析响应内容
        response_data = response.json()
        print(response_data)

        # 断言状态为fail
        self.assertEqual(response_data['status'], 'fail')

    # 测试用例方法5：更新成功
    def test_updateadmin_success(self):
        # 准备测试数据 - 使用确实存在的ID
        data = {
            "id": "9",  # 修改为确实存在的ID
            "account": "admin002",
            "password": "newpass123",
            "rid": "1",
            "createTime": "2023-05-21 10:15:00",
            "describe": "已更新的财务管理员",
            "status": "正常"
        }

        # 发起更新admin的请求
        response = self.session.post(self.url_updateadmin, json=data)

        # 检查响应状态码是否为200
        self.assertEqual(response.status_code, 200)

        # 解析响应内容
        response_data = response.json()

        print(response_data)
        # 断言状态为success
        self.assertEqual(response_data['status'], 'success')

    # 测试用例方法6：更新失败
    def test_updateadmin_fail(self):
        # 准备测试数据
        data = {
            "id":"1200",
            "account": "admin126",
            "password": "000000",
            "rid": "other",
            "createTime": "2024-01-01 00:00:00",
            "describe": "普通管理员",
            "status": "1"
        }

        # 发起添加admin的请求
        response = self.session.post(self.url_updateadmin, json=data)

        # 检查响应状态码是否为200
        self.assertEqual(response.status_code, 200)

        # 解析响应内容
        response_data = response.json()

        print(response_data)
        # 断言状态为fail
        self.assertEqual(response_data['status'], 'fail')

    # 测试用例方法7：查找成功
    def test_find_success(self):
        # 构造管理员id数据
        admin_id = 9

        # 发起查询管理员请求
        find_data = {'id': admin_id}
        response = self.session.get(self.url_findadmin, params=find_data)

        # 解析响应内容
        response_data = response.json()
        print(response_data)

        # 断言状态为success
        self.assertEqual(response_data['status'], 'success')

    # 测试用例方法8：查找失败
    def test_find_fail(self):  # 修改方法名，原来有两个test_delete_fail
        # 构造管理员id数据
        admin_id = 999  # 使用不存在的ID

        # 发起查询管理员请求
        find_data = {'id': admin_id}
        response = self.session.get(self.url_findadmin, params=find_data)

        # 解析响应内容
        response_data = response.json()
        print(response_data)

        # 断言状态为fail
        self.assertEqual(response_data['status'], 'fail')

if __name__ == '__main__':
    unittest.main()